package com.bindapp.model;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bindapp.R;
import com.bindapp.databinding.ActivityJsBinding;
import com.github.lzyzsd.circleprogress.DonutProgress;
import com.warkiz.widget.RangeSeekBar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by apple on 2018/7/9.
 */

public class ControllerJsModel extends BaseModel {
    private ActivityJsBinding binding;
    private List<TextView> textViewList=new ArrayList<TextView>();
    private List<DonutProgress> passlist=new ArrayList<DonutProgress>();

    private TimePickerView timePickerView;

    private SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm");

    public ControllerJsModel(Context context){
        super(context);
        binding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.activity_js,null,false);
        binding.setClick(new ViewClick());
        binding.spanner1.setTag(0);
        textViewList.add(binding.textview1);
        textViewList.add(binding.textview2);
        textViewList.add(binding.textview3);

        passlist.add(binding.progressview1);
        passlist.add(binding.progressview2);
        passlist.add(binding.progressview3);

        timePickerView=new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                Log.e("Mytext",""+date.getTime());
            }
        })
                .setType(new boolean[]{false, true, true, true, true, false})
                .setCancelText("取消")
                .setSubmitText("确定")
                .setOutSideCancelable(false)
                .setTitleText("请选择时间")
                .isCyclic(true)
                .isDialog(false)
                .build();

        binding.seekbar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener() {
            @Override
            public void onRangeSeekBarValuesChanged(RangeSeekBar bar, Number minValue, Number maxValue) {
                Log.e("Mytext",minValue.toString()+"--->"+maxValue.toString());
                if(Double.valueOf(minValue.toString())>=Double.valueOf(maxValue.toString())){
                    binding.fdtap.setChecked(false);
                }else{
                    binding.fdtap.setChecked(true);
                }
            }
        });
    }

    @Override
    public ActivityJsBinding getBinding() {
        return binding;
    }


    public class ViewClick{
        public void onClick(View view){
            switch(view.getId()){
                case R.id.spanner1:
                    if(((int)binding.spanner1.getTag())==0){
                        binding.spanner1.setTag(1);
                        binding.spanner1.setBackground(ContextCompat.getDrawable(context,R.drawable.round_blue_back));
                    }else{
                        binding.spanner1.setTag(0);
                        binding.spanner1.setBackground(ContextCompat.getDrawable(context,R.drawable.round_light_gray));
                    }
                    break;
                case R.id.spanner2:
                    new TimePickerBuilder(context, new OnTimeSelectListener() {
                        @Override
                        public void onTimeSelect(Date date, View v) {
                            String result = format.format(date);
                            Log.e("Mytext",""+result);
                            binding.spanner2.setText(result);
                            binding.spanner2.setTextSize(12);
                            binding.spanner2.setGravity(Gravity.CENTER);
                        }
                    })
                            .setType(new boolean[]{true, true, true, true, true, false})
                            .setCancelText("取消")
                            .setSubmitText("确定")
                            .setOutSideCancelable(false)
                            .setTitleText("请选择时间")
                            .isCyclic(true)
                            .isDialog(false)
                            .build().show();
                    break;
                case R.id.layout1:
                    setSelection(binding.textview1,binding.progressview1);
                    binding.progressview1.setTextColor(ContextCompat.getColor(context,R.color.red_btn_bg_color));
                    binding.progressview1.setFinishedStrokeColor(ContextCompat.getColor(context,R.color.red_btn_bg_color));
                    break;
                case R.id.layout2:
                    setSelection(binding.textview2,binding.progressview2);
                    binding.progressview2.setTextColor(ContextCompat.getColor(context,R.color.light_green));
                    binding.progressview2.setFinishedStrokeColor(ContextCompat.getColor(context,R.color.light_green));
                    break;
                case R.id.layout3:
                    setSelection(binding.textview3,binding.progressview3);
                    binding.progressview3.setTextColor(ContextCompat.getColor(context,R.color.seelbar));
                    binding.progressview3.setFinishedStrokeColor(ContextCompat.getColor(context,R.color.seelbar));
                    break;
            }
        }
    }

    private void setSelection(TextView textview,DonutProgress textpass){
        for(TextView view:textViewList){
            view.setTextColor(ContextCompat.getColor(context,R.color.textcolor));
            view.setBackground(null);
        }
        textview.setTextColor(Color.WHITE);
        textview.setBackground(ContextCompat.getDrawable(context,R.drawable.round_yellow));
        for(DonutProgress view:passlist){
            if(view!=textpass){
                view.setTextColor(ContextCompat.getColor(context,R.color.textcolor));
                view.setFinishedStrokeColor(ContextCompat.getColor(context,R.color.linecolor));
            }
        }

    }

}
